package com.winway.sixbook.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.winway.sixbook.HomeActivity;
import com.winway.sixbook.R;
import com.winway.sixbook.custom.CalendarView;
import com.winway.sixbook.custom.CalendarView.OnItemClickListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class CalendarActivity extends Activity {
	
	private static final String TAG = "CalendarActivity";
	private CalendarView calendar = null;
	private ImageButton calendarLeft;
	private ImageButton calendarRight;
	private TextView calendarCenter;
	
	private ImageButton top_tbn_fanhui;
	private Button top_tbn_xinzeng;
	private TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calendar);
		calendarLeft = (ImageButton)findViewById(R.id.calendarLeft);
		calendarRight = (ImageButton)findViewById(R.id.calendarRight);
		calendarCenter = (TextView)findViewById(R.id.calendarCenter);
		top_tbn_fanhui = (ImageButton)findViewById(R.id.nagivate_btn_left);
        top_tbn_xinzeng = (Button)findViewById(R.id.navigate_btn_right);
        title = (TextView)findViewById(R.id.title);
		
        top_tbn_xinzeng.setVisibility(View.INVISIBLE);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
		calendarCenter.setText(sdf.format(new Date()));
		
		//获取日历控件对象  
		calendar = (CalendarView)findViewById(R.id.calendar);  
		//获取日历中年月 ya[0]为年，ya[1]为月（格式大家可以自行在日历控件中改）  
		String[] ya = calendar.getYearAndmonth().split("-");    
		//点击上一月 同样返回年月   
		String leftYearAndmonth = calendar.clickLeftMonth();   
		String[] lya = leftYearAndmonth.split("-");  
		//点击下一月  
		String rightYearAndmonth = calendar.clickRightMonth();   
		String[] rya = rightYearAndmonth.split("-");  
		//设置控件监听，可以监听到点击的每一天（大家也可以在控件中自行设定）  
		calendar.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void OnItemClick(Date date) {
//				 Toast.makeText(getApplicationContext(), date+"", Toast.LENGTH_SHORT).show();
				 if(date.after(new Date())){
					 Toast.makeText(getApplicationContext(), "只能查询今日之前的信息！", Toast.LENGTH_SHORT).show();
				 }else{
					 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
					 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
					 calendarCenter.setText("当前月份："+sdf.format(date));
					 
//					 Intent queryintent = new Intent(CalendarActivity.this, TodayListNewActivity.class);
					 Intent queryintent = new Intent(CalendarActivity.this, GridActivity.class);
					 Bundle bundle = new Bundle(); 
					 bundle.putString("Addtime", sdf2.format(date));
					 queryintent.putExtras(bundle); 
					 startActivity(queryintent);
				 }
			}
		}); 
		calendarLeft.setOnClickListener(mListener);
		calendarRight.setOnClickListener(mListener);
		top_tbn_fanhui.setOnClickListener(mListener);
        title.setText("历史");
	}
	OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.calendarLeft:
				String newString = calendar.clickLeftMonth(); 
				calendarCenter.setText("当前月份："+newString);
				break;
			case R.id.calendarRight:
				newString = calendar.clickRightMonth();
				calendarCenter.setText("当前月份："+newString);
				break;
			case R.id.nagivate_btn_left:
				Intent counter = new Intent(CalendarActivity.this, HomeActivity.class);
				startActivity(counter);
				finish();
				break;
			}
		}
	};

}
